package com.codez.gznx.excel;

import java.text.SimpleDateFormat;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author dechao.zheng
 * @createTime: 2019-09-24 11:08:24	
 *
 */

@Slf4j
public class DateUtils {

	private static String[] parttens = {
			"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyy-MM", 
            "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd", "yyyy/MM",
            "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM.dd", "yyyy.MM",
            "yyyyMMdd" , "yyyyMM" 
	};

	public static Object getDate(String dateStr) throws ValidateEntityException {
		SimpleDateFormat parser = null;
		for (String ptn : parttens) {
			try {
				if(ptn.length() != dateStr.length()) {
					continue;
				}
				new SimpleDateFormat(ptn).parse(dateStr);
				parser = new SimpleDateFormat(ptn);
				log.debug(" 创建时间解析器成功 -- {}" , ptn);
				return parser.parse(dateStr);
			} catch (Exception e) {
			}
		}
		log.error(" 解析时间[{}]发生异常" , dateStr);
		if(parser == null){
			throw new ValidateEntityException("[{0}]时间格式错误") ;
		}

		return null;
	}
	
	
	
}
